package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    /**
     * 进行客户列表分页（名称模糊查询）
     *
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @Override
    public Map<String, Object> getCustomerList(Integer page, Integer rows, String customerName) {
        HashMap<String, Object> map = new HashMap<>();
        page = page == 0 ? page + 1 : page;
        int getCount = (page - 1) * rows;
        List<Customer> customerList = customerDao.getCustomerList(getCount, rows, customerName);
        if (!CollectionUtils.isEmpty(customerList)) {
            map.put("total", customerList.size());
            map.put("rows", customerList);
        }
        return map;
    }
    /**
     * 客户添加或修改
     * @param customer
     * @return
     */
    @Override
    public ServiceVO customerSave(Customer customer) {
        Integer customerId = customer.getCustomerId();
        if (customerId!=null){
            //修改
            customerDao.updateCustomer(customer);
        }else {
            //保存
            customerDao.save(customer);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public ServiceVO customerDelete(String ids) {
        String[] split = ids.split(",");
        if (split.length>0&&split!=null) {
            for (String id : split) {
                customerDao.delete(id);
            }
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
}
